<?php defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
/**
 * Title          ads
 *
 * @package        LMJ-futureid
 * Location        application/frontend/libraries/mobile
 *
 * @author         azeem 
 * @copyright      Cyber L-M-J
 * Description     this is for the ads(offers promotion/deals)
 *
 **/

class Image_upload {
    //put your code here
    protected $ci;
    
    protected $upload_config = array();
    protected $media_path;
    protected $media_web_path;
    protected $album_types = array('profile'=>1,'billboard'=>2);
    
	public function __construct() {
		$this->ci =& get_instance();
		$this->media_path = $this->ci->config->item('upload_path');
		$this->media_web_path = $this->ci->config->item('media_web_path');
		$this->upload_config['allowed_types'] = $this->ci->config->item('allowed_types');
		$this->upload_config['max_size'] = $this->ci->config->item('max_size');
		$this->upload_config['max_width'] = $this->ci->config->item('max_width');
		$this->upload_config['max_height'] = $this->ci->config->item('max_height');
		$this->ci->load->library('upload');
		$this->ci->load->library('photo/Album');
		$this->ci->load->library('photo/Photo');
	}
	
	public function profile_image_upload($data) {
		$response['status'] = 0;
	    $response['message']='Sorry! Image upload fail';
		$config = $this->upload_config;
		$business_id = $this->ci->input->post('business_id');
		$config['upload_path'] = $this->media_path.'business/albums/'.md5($business_id).'/default/';
		$config['file_name'] = md5(time());
		$feild_name = $data['feild_name'];
		$this->ci->upload->initialize($config);
		$this->create_destination_path($config['upload_path']);
		if ($this->ci->upload->do_upload($feild_name)){
			if(!$this->ci->album->is_available($business_id,'DEFAULT',1)){
				$album_data = array('AlbumName'=>'DEFAULT','BusinessID'=>$business_id,'AlbumTypeID'=>1,'CreatedBy'=>$business_id,'CreatedDate'=>time());
				$album_id = $this->ci->album->create($album_data);
			} else {
				$album_data = $this->ci->album->get_album_by_filters($business_id,'DEFAULT',1);
				$album_id = $album_data['AlbumID'];
			}
			$upload_data = $this->ci->upload->data();
			$image_data = array('AlbumID'=>$album_id,'ImageUrl'=>$upload_data['file_name'],'ImageText'=>'','LinkTypeID'=>'','CreatedBy'=>$business_id,'CreatedDate'=>time());
			$id = $this->ci->photo->add($image_data);
			$this->ci->photo->update_business($business_id,array('ProfileAlbumID'=>$album_id,'ProfileImage'=>$upload_data['file_name']));
			if($id && $album_id){
				$response['status'] = 1;
				$image_url = $this->media_web_path.'photos/fine_cropped/200/200/business/albums/'.md5($business_id).'/default/'.$upload_data['file_name'];
				$result = array('ImageUrl'=>$image_url);
				$response['message'] = $result;
			}
			
		}
		return $response;
	}
	
	public function create_destination_path($path){
		if(!is_dir($path)){
			return mkdir($path, DIR_WRITE_MODE,TRUE);
		} else {
			return FALSE;
		}
	}

	public function billboard_image_upload($data) {
		$response['status'] = 0;
	    $response['message']='Sorry! Image upload fail';
		$config = $this->upload_config;
		$business_id = $this->ci->input->post('business_id');
		$config['upload_path'] = $this->media_path.'business/albums/'.md5($business_id).'/billboard/';
		$config['file_name'] = md5(time());
		$feild_name = $data['feild_name'];
		$this->ci->upload->initialize($config);
		$this->create_destination_path($config['upload_path']);
		if ($this->ci->upload->do_upload($feild_name)){
			if(!$this->ci->album->is_available($business_id,'BILLBOARD',2)){
				$album_data = array('AlbumName'=>'BILLBOARD','BusinessID'=>$business_id,'AlbumTypeID'=>2,'CreatedBy'=>$business_id,'CreatedDate'=>time());
				$album_id = $this->ci->album->create($album_data);
			} else {
				$album_data = $this->ci->album->get_album_by_filters($business_id,'BILLBOARD',2);
				$album_id = $album_data['AlbumID'];
			}
			$upload_data = $this->ci->upload->data();
			$image_data = array('AlbumID'=>$album_id,'ImageUrl'=>$upload_data['file_name'],'ImageText'=>'','LinkTypeID'=>'','CreatedBy'=>$business_id,'CreatedDate'=>time());
			$id = $this->ci->photo->add($image_data);
			$this->ci->photo->update_business($business_id,array('ProfileAlbumID'=>$album_id,'ProfileImage'=>$upload_data['file_name']));
			if($id && $album_id){
				$response['status'] = 1;
				$image_url = $this->media_web_path.'photos/fine_cropped/200/200/business/albums/'.md5($business_id).'/billboard/'.$upload_data['file_name'];
				$result = array('AlbumImageID'=>$id,'AlbumID'=>$album_id,'ImageUrl'=>$image_url);
				$response['message'] = $result;
			}
			
		}
		return $response;
		 
	}
	
    
}